package com.sun.dailyprj.designpatterns.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/*
 * =====================================================================================
 * Summary:
 *
 * File: JDKProxyUser.java
 * Author: Yanpeng.Sun
 * Create: 2019/10/31 16:41
 * =====================================================================================
 */
public class JDKProxyUser {
    public static void main(String[] args) {
        ShopPhone shopPhone = new ShopPhone();
        shopPhone.shopPhone("在大陆买了一个iPhone X，一共花了8888元");
        /*动态代理---在香港买手机*/
        IShopPhone proxy = (IShopPhone) Proxy.newProxyInstance(shopPhone.getClass().getClassLoader(),
                shopPhone.getClass().getInterfaces(), new InvocationHandler(){
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("-----动态代理 start----");
                        Object returnValue = method.invoke(shopPhone, args);
                        System.out.println("-----动态代理 end----");
                        return returnValue;
                    }
                });
        proxy.shopPhone("通过代购，在香港买了一个iphone X，一共花了6666元");
    }
}
